package com.zlq.cn.controller.sign.lovers;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlq.cn.api.SignApiResult;
import com.zlq.cn.bootstrap.SignApiBootstrap;
import com.zlq.cn.entity.LoversAnniversaryDay;
import com.zlq.cn.entity.User;
import com.zlq.cn.service.lovers.LoversAnniversaryDayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * <p>
 * 纪念日 前端控制器
 * </p>
 *
 * @author zhoucl
 * @since 2022-05-08
 */
@Api(tags = "小情侣-纪念日")
@RestController
@RequestMapping("/lovers/anniversary/day")
public class LoversAnniversaryDayController {

    @Resource
    private LoversAnniversaryDayService loversAnniversaryDayService;

    @ApiOperation("新增")
    @PostMapping("/add")
    public SignApiResult<String> add(@Valid @RequestBody LoversAnniversaryDay loversAnniversaryDay) {
        User user = SignApiBootstrap.requestUser();
        loversAnniversaryDay.setUserId(user.getId())
                .setCreateTime(LocalDateTime.now())
                .setUpdateTime(LocalDateTime.now());
        loversAnniversaryDayService.save(loversAnniversaryDay);
        return SignApiResult.resultSuccess("新增成功");
    }

    @ApiOperation(value = "编辑", notes = "跟新增不同的是需要传递id")
    @PostMapping("/edit")
    public SignApiResult<String> edit(@Valid @RequestBody LoversAnniversaryDay loversAnniversaryDay) {
        Assert.notNull(loversAnniversaryDay.getAnniversaryDayId(), "纪念日id不能为空");
        User user = SignApiBootstrap.requestUser();
        loversAnniversaryDay
                .setUpdateTime(LocalDateTime.now());
        loversAnniversaryDayService.updateById(loversAnniversaryDay);
        return SignApiResult.resultSuccess("新增成功");
    }

    @ApiOperation(value = "删除", notes = "只需要传递id即可")
    @PostMapping("/remove")
    public SignApiResult<String> remove(@Valid @RequestBody LoversAnniversaryDay loversAnniversaryDay) {
        loversAnniversaryDayService.removeById(loversAnniversaryDay.getAnniversaryDayId());
        return SignApiResult.resultSuccess("删除成功");
    }


    @ApiOperation(value = "获取纪念日列表")
    @PostMapping("/list")
    public SignApiResult<Page<LoversAnniversaryDay>> list(@RequestBody Page<LoversAnniversaryDay> page) {
        return SignApiResult.resultSuccess(loversAnniversaryDayService.pageAnniversaryDay(page));
    }
}

